include "./global.f95"
module routine
    use variables
    interface output
        module procedure output_int
        module procedure output_real
        module procedure output_real_array
    end interface
contains
    subroutine init
        integer(kind=4) :: i
        t=0
        forall(i=1:NX)
            x(i)=i*Dx+x_range(1)
        end forall
        !u=0.0
        !where(x<0) u=1.0
        u=1.5+0.5*tanh(x)
        call getA
        reccount=1
    end subroutine
    
    subroutine getA
        implicit none
        a = u
    end subroutine
    
    subroutine Lax_Wendroff
        real(kind=4) :: ut(1:NX),at(NX)
        ut(1:NX)=u(1:NX)
        !ut(0)=u(1)
        !ut(NX+1)=u(NX)
        dt=CFL*Dx/maxval(abs(a))
        at=a*dt/Dx
        !u(1:NX)=ut(1:NX)-at/2.0*(ut(2:NX+1)-ut(0:NX-1))+0.5*at**2*(ut(2:NX+1)-2.0*ut(1:NX)+ut(0:NX-1))
        u(2:NX-1)=ut(2:NX-1)-at(2:NX-1)/2.0*(ut(3:NX)-ut(1:NX-2))+0.5*(at(2:NX-1)**2)*(ut(3:NX)-2.0*ut(2:NX-1)+ut(1:NX-2))
        u(1)=u(2)
        u(NX)=u(NX-1)
        t=t+dt
    end subroutine

    subroutine output_int(u,x)
        implicit none
        integer :: u
        integer(kind=4) :: x
        write(unit=u,rec=reccount)x
        reccount=reccount+1
    end subroutine

    subroutine output_real(u,x)
        implicit none
        integer :: u
        real(kind=4) :: x
        write(unit=u,rec=reccount)x
        reccount=reccount+1
    end subroutine

    subroutine output_real_array(u,x)
        implicit none
        integer :: u, i
        real(kind=4) :: x(NX)
        do i=1,NX
            write(unit=u,rec=reccount)x(i)
            reccount=reccount+1
        end do
    end subroutine
end module
